﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;

namespace BaiduMusic_Downloader
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            musicListBox.Items.Clear();
            if (titleTextBox.Text.Equals(""))
            {
                MessageBox.Show("Music name is null!!!");
                return;
            }

            string url = "http://box.zhangmen.baidu.com/x?op=12&count=1&title=" + titleTextBox.Text + "$$" + artistTextBox.Text + "$$$$";
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(url);
            XmlNodeList nodeList = xmlDocument.SelectNodes("/result/url");
            foreach (XmlNode node in nodeList)
            {
                string encodeItem = node.SelectSingleNode("encode").InnerText;
                string decodeItem = node.SelectSingleNode("decode").InnerText;
                if (!(encodeItem.IndexOf(".mp3") > 0 || encodeItem.IndexOf(".wma") > 0))
                {
                    string musicurl = encodeItem.Remove(encodeItem.LastIndexOf("/") + 1) + decodeItem;
                    musicListBox.Items.Add(musicurl);
                }
            }
        }

        private void 下载ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (musicListBox.SelectedItem!=null)
            {
                saveFileDialog.FileName = titleTextBox.Text + "-" + artistTextBox.Text;
                saveFileDialog.ShowDialog();
            }
            else
            {
                MessageBox.Show("先选一个下载连接啊！");
            }
        }

        private void saveFileDialog_FileOk(object sender, CancelEventArgs e)
        {
            WebClient webclient = new WebClient();
            Uri musicurl = new Uri(musicListBox.SelectedItem.ToString());
            webclient.DownloadFileAsync(musicurl, saveFileDialog.FileName);
            webclient.DownloadProgressChanged += webclient_DownloadProgressChanged;
            webclient.DownloadFileCompleted += webclient_DownloadFileCompleted;
        }

        #region Events
        void webclient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            Action<DownloadProgressChangedEventArgs> OnChange = ProgressChange;
            OnChange.Invoke(e);
        }

        void webclient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            //itemProgressBar.Value = 0;
            //logTextBox.AppendText(" Download success!\r\n");
        }

        void ProgressChange(DownloadProgressChangedEventArgs e)
        {
            progressBar.Value = e.ProgressPercentage;
        }
        #endregion
    }
}
